package org.zhaozhen.user.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.zhaozhen.basic.businessexception.BusinessException;
import org.zhaozhen.basic.util.AjaxResult;
import org.zhaozhen.basic.util.PageList;
import org.zhaozhen.user.domain.User;
import org.zhaozhen.user.dto.UserDto;
import org.zhaozhen.user.query.UserQuery;
import org.zhaozhen.user.service.IUserService;

import java.util.List;

@RestController
@RequestMapping("/user")
@Api(tags = "用户接口",description = "用户接口详细描述")
public class UserController {
    @Autowired
    private IUserService userService;

    @PostMapping("/phone/register")
    public AjaxResult register(@RequestBody UserDto dto){
        try {
            userService.register(dto);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me()
                    .setSuccess(false)
                    .setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me()
                    .setSuccess(false)
                    .setMessage("噢哟,报错了,和我们没关系哈");
        }
    }


    /**
     * 新增或者修改用户信息
     * @param user
     * @return
     */
    @PutMapping
    @ApiOperation(value = "用户添加或修改",notes = "如果有id就是修改否则就是添加")
    public AjaxResult addOrUpdate(@RequestBody User user){
        try {
            if (user.getId()==null){
                userService.add(user);
            }else {
                userService.update(user);
            }
            return AjaxResult.me();
        }catch(Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙");
        }
    }

    /**
     * 根据ID删除一条数据
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public AjaxResult removeById(@PathVariable("id") Long id){
        try {
            userService.remove(id);
            return AjaxResult.me();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙");
        }
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    public AjaxResult batchRemove(@RequestBody List<Long> ids){
        try {
            userService.batchRemove(ids);
            return AjaxResult.me();
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult
                    .me()
                    .setSuccess(false)
                    .setMessage("系统繁忙，稍后重试！！！！");
        }
    }

    /**
     * 根据ID查询一条数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public User queryById(@PathVariable("id") Long id){
        return userService.queryById(id);
    }

    /**
     * 查询所有数据
     * @return
     */
    @GetMapping
    public List<User> queryAll(){
        return userService.queryAll();
    }
    @PostMapping("/list")
    public PageList<User> queryPage(@RequestBody UserQuery query){
        return userService.queryPage(query);
    }
}

